package cate.game.role.pvp.videohall;

import cate.common.table.d.GDObj;
import cate.common.table.d.GDVideoHall;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.pvp.videohall.msg.*;
import cate.game.videohall.po.VideoGroupPO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

public class RoleVideoHall extends RoleDependent {
    @NoteField(value = "今日点赞的录像id")
    public List<String> admireUids;

    @NoteField(value = "我的录像快照组")
    public List<VideoGroupPO> groups;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (admireUids == null) {
            admireUids = new ArrayList<>();
        }

        if (groups == null) {
            groups = new ArrayList<>();
        }
        groups.forEach(VideoGroupPO::initialize);
    }

    @JsonIgnore
    public synchronized VideoGroupPO getGroup(GDVideoHall.Group group) {
        for (VideoGroupPO groupPO : groups) {
            if (groupPO.group == group.getId()) {
                return groupPO;
            }
        }
        VideoGroupPO groupPO = new VideoGroupPO(group.getId());
        groups.add(groupPO);
        return groupPO;
    }

    @Override
    public void onDaySpan(boolean silence) {
        if (!admireUids.isEmpty()) {
            admireUids.clear();
            if(!silence){
                role.sendNow(new MyAdmireDataResp(admireUids));
            }
        }
        groups.forEach(VideoGroupPO::removeExpired);
    }

    public void noticeUpdate(){
        role.sendNow(new MyAdmireDataResp(admireUids));
    }

    private GameResult<Void> admireEheck(String replayUid) {
        GameResult<Void> r = new GameResult<>();
        if (admireUids.size() >= GDVideoHall.ADMIRE_DAILY) {
            return r.fail("今日已无点赞次数");
        }
        if (admireUids.contains(replayUid)) {
            return r.fail("该录像已点赞");
        }
        return r.success();
    }

    private void afterAdmire(String replayUid) {
        this.admireUids.add(replayUid);
        MixRes reward = new MixRes(GDObj.getMixResStr(GDObj.Type.NUM, GDObj.Num.M0, GDVideoHall.ADMIRE_REWARD_NUM));
        reward.add(role, true, 0);
        role.getGame().notice.message(role, "点赞成功，获得 "
                + reward.items.get(0).num
                + reward.items.get(0).description(role.getGame()));
        role.sendNow(new MyAdmireDataResp(admireUids));
    }

    public GameResult<Void> admire(String replayUid){
        GameResult<Void> r = admireEheck(replayUid);
        if (!r.ok()) {
            return r.fail(r.message);
        }
        int admireTimes = role.getGame().videoHall.admire(role, replayUid);
        role.sendNow(new AdmireVideoResp(admireTimes, replayUid));
        afterAdmire(replayUid);
        return r;
    }

    public GameResult<Void> cancleCollect(String replayUid){
        GameResult<Void> r = role.getGame().videoHall.cancelCollect(role, replayUid);
        if (!r.ok()) {
            return r.fail(r.message);
        }
        role.getGame().notice.message(role, "已取消收藏!");
        role.sendNow(new CancleCollectVideoResp(replayUid));
        return r;
    }

    public GameResult<Void> collect(String replayUid){
        GameResult<Void> r = role.getGame().videoHall.collect(role, replayUid);
        if (!r.ok()) {
            return r;
        }
        role.getGame().notice.message(role, "收藏成功!");
        role.sendNow(new CollectVideoResp(replayUid));
        return r;
    }
}
